// pages/feedback1/feedback1.js
var app = getApp()
const db = wx.cloud.database()
const _ = db.command;
let nickname = ''
let contactway = ''
let infor = ''
let block = ''
var block1 = '全部'
const array1 = ['全部', '预警信息', '求助信息', '科普知识', '其他']
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: getApp().globalData.username,
    array: ['全部', '预警信息', '求助信息', '科普知识', '其他'],
    index: 0,
    isHide: false,
    nickname: '',
    contactway: '',
    infor: '',
    block: '',
    hidewarning: true,
    hidewarning1: true,
  },

  navigateBack:function(){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   let username=app.globalData.username
   this.setData({
    username:app.globalData.username
  })
  },

  listenerPickerSelected: function (e) {
    //改变index值，通过setData()方法重绘界面
    this.setData({
      index: e.detail.value,
    })
    let index = e.detail.value
    block1 = array1[index]
  },
  getNickname(e) {
    nickname = e.detail.value;
    var that = this;
    if (e.detail.cursor > 0) {
      that.setData({
        hidewarning: true
      })
    } else {
      that.setData({
        hidewarning: false
      })
    }
  },
  getContactWay(e) {
    contactway = e.detail.value
  },
  getInfor(e) {
    infor = e.detail.value
    var that = this;
    if (e.detail.cursor > 0) {
      that.setData({
        isHide: true
      })
    } else {
      that.setData({
        isHide: false
      })
    }
    if (e.detail.cursor > 0) {
      that.setData({
        hidewarning1: true
      })
    } else {
      that.setData({
        hidewarning1: false
      })
    }
  },
  submit: function () {
    var year = new Date().getFullYear() // 示例
    var month = new Date().getMonth() + 1
    var day = new Date().getDate()
    var hours = new Date().getHours()
    var minutes = new Date().getMinutes()
    var seconds = new Date().getSeconds()
    const time= year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
    console.log(nickname + block1 + contactway + infor+time)
    if ((nickname.length > 0||nickname==this.data.username) && infor.length > 0) {
      db.collection('eas-feedback').add({
        data: {
          nickname: nickname,
          contactway: contactway,
          block: block1,
          infor: infor,
          time: year + '.' + month + '.' + day + ' ' + hours + ':' + minutes + ':' + seconds
        },
        success: res => {
          wx.showToast({
            title: '感谢你的反馈',
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
          
        },

        fail: res => {
          wx.showToast({
            title: '提交失败',
            icon: 'error'
          })
        }
      })
    }
    else{
      wx.showToast({
        title: '请完整填写',
        icon:'error'
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})